#!/usr/bin/python

import sys
import os
import csv

import common

common.setupEnv(globals())

usage = "Generate report for a financial year"

parser = common.StockieOptionParser(usage=usage)
parser.add_posarg('year', dest="year", type="int")
options, args = parser.parse_args()

rows = interface.prepareFinYearReport(options.year)
reportfile = "report%s.csv" % options.year
if os.path.exists(reportfile):
    sys.exit("%s exists. Please remove it and run again" % reportfile)
writer = csv.writer(open(reportfile, "w"))
writer.writerows(rows)
print "Report saved to %s" % reportfile
